﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ThirdPersonSolution : MonoBehaviour
{
    [SerializeField] Joystick joystick;
    [SerializeField] AtkJoyStick AtkJoystick;
    [SerializeField] GameObject model;
    CharacterController controller;
    public bool isAttacking;
    public bool isMoving;

    void Start()
    {
        controller = GetComponent<CharacterController>();

        joystick.OnValueChanged.AddListener(v =>
        {
            isMoving = v.magnitude != 0 || Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0;
            if (v.magnitude != 0)
            {
                Vector3 direction = new Vector3(v.x, 0, v.y);
                controller.Move(direction.normalized * GetComponent<Player>().speed * Time.deltaTime);

                if (!isAttacking)
                    model.transform.rotation = Quaternion.LookRotation(direction);
            }
            else if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
            {
                Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
                controller.Move(direction.normalized * GetComponent<Player>().speed * Time.deltaTime);
                if (!isAttacking)
                    model.transform.rotation = Quaternion.LookRotation(direction);
            }

        });

        AtkJoystick.OnValueChanged.AddListener(v =>
        {
            isAttacking = v.magnitude != 0;
            model.transform.Find("arrow").gameObject.SetActive(isAttacking);
            if (isAttacking)
            {
                model.transform.rotation = Quaternion.LookRotation(new Vector3(v.x, 0, v.y));
            }

        });

        AtkJoystick.OnPointerUp.AddListener(v =>
        {
            GetComponent<Player>().Attack();
        });


        Animator anim = Player.PlayerPrefab.GetComponent<Animator>();
        if (joystick)
        {
            joystick.OnPointerDown.AddListener(v =>
            {
                if (anim)
                    anim.SetBool("walking", true);
            });
            joystick.OnPointerUp.AddListener(v =>
            {
                if (anim)
                    anim.SetBool("walking", false);
            });
        }

        if (AtkJoystick)
        {
            AtkJoystick.OnPointerDown.AddListener(v =>
        {
            if (anim)
                anim.SetBool("attack", true);
        });
            AtkJoystick.OnPointerUp.AddListener(v =>
            {
                if (anim)
                    anim.SetBool("attack", false);
            });
        }

        StartCoroutine(WalkEffect());
    }


    /// <summary> 角色移动经过时生成的物体 </summary>
    /// <returns> 调用延迟协程 </returns>
    IEnumerator WalkEffect()
    {
        yield return new WaitForSeconds(0.2f);
        List<GameObject> rootPref = GetComponent<Player>().rootPrefabs;
        if (isMoving && rootPref.Count != 0)
        {
            foreach (var item in rootPref)
            {
                
                   GameObject temp= Instantiate(item, model.transform.Find("root").transform.position,
                    model.transform.Find("root").transform.rotation);
                    temp.transform.GetComponent<Thorn>().CharCollider = this.GetComponent<CharacterController>();
            }
        }

        yield return StartCoroutine(WalkEffect());
    }
}

